package com.hysoft.service.Impl;

import com.hysoft.service.ConnectionTester;
import com.hysoft.util.JdbcUtil;
import org.springframework.stereotype.Service;


import java.sql.*;
@Service
public class ConnectionTesterImpl extends JdbcUtil implements ConnectionTester {


    public String testOracleConnection(String host, String port, String SID, String username, String password) {
        String  driver = "oracle.jdbc.driver.OracleDriver";
        String msg = "连接成功";
        Connection connection = null;
        if (host == null||host == ""||host.equalsIgnoreCase("")||host.equalsIgnoreCase(null)
        &&port == null||port == ""||port.equalsIgnoreCase("")||port.equalsIgnoreCase(null)
        &&SID == null||SID == ""||SID.equalsIgnoreCase("")||SID.equalsIgnoreCase(null)
        &&username == null||username == ""||username.equalsIgnoreCase("")||username.equalsIgnoreCase(null)
        &&password == null||password == ""||password.equalsIgnoreCase("")||password.equalsIgnoreCase(null)){
            return "字段不允许为空！";
        }else {
            try {
                Class.forName(driver);
                connection = DriverManager.getConnection("jdbc:oracle:thin:@"+host+":"+port+":"+SID,username,password);
            } catch (ClassNotFoundException e) {
                msg = "连接失败";
                e.printStackTrace();
            } catch (SQLException e) {
                msg = "连接失败";
                e.printStackTrace();
            }
        }
        return msg;
    }


    public String testAccessConnection(String path) {
        Connection accessConnection = getAccessConnection(path);
        if (accessConnection != null){
            try {
                accessConnection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return "连接成功";
        }else{
            return "连接失败";
        }
    }


    public Connection getAccessConnection(String path){
        Connection accessConnection = super.getAccessConnection(path);
        return accessConnection;
    }


    public Connection getOracleConnection(String host, String port, String SID, String username, String password) {
        Connection oracleConnection = super.getOracleConnection(host, port, SID, username, password);
            return oracleConnection;
    }
}
